#include <string>
#include "atomic.h"
#include "message.h"
#include "mainsimu.h"
#include "strutil.h"
#include "../HeadFile/PCS_tank_storage_18_model.h"


/**** the Constructor ****/
PCS_tank_storage_18_model::PCS_tank_storage_18_model( const string &name)
: Atomic( name )
, PCS_tank_18_stream_GB90_out( this->addInputPort( "PCS_tank_18_stream_GB90_out") )
, PCS_tank_18_stream_GB90_1( this->addInputPort( "PCS_tank_18_stream_GB90_1") )
, PCS_tank_18_stream_GB90_2( this->addOutputPort( "PCS_tank_18_stream_GB90_2") )
, PCS_tank_18_stream_GB90_inv( this->addOutputPort( "PCS_tank_18_stream_GB90_inv") )
{

        
    string tmpval(MainSimulator::Instance().getParameter( this->description(), "PCS_tank_18_stream_GB90_inv_val" ));
    PCS_tank_18_stream_GB90_inv_val=str2float(tmpval);
        
        
}


/*** the initFunction ***/
Model &PCS_tank_storage_18_model::initFunction()
{

        
    count = 0;
    delay = "00:00:00:00";
    passivate();
    return *this;
        
        
}

/*** the externalFunction ***/
Model &PCS_tank_storage_18_model::externalFunction( const ExternalMessage &msg )
{

           
    if( msg.port() == PCS_tank_18_stream_GB90_1 ){
        PCS_tank_18_stream_GB90_1_flowrate = msg.value();
        count++;
        holdIn( active, delay);
    }

    if( msg.port() == PCS_tank_18_stream_GB90_out ){
        PCS_tank_18_stream_GB90_out_flowrate = msg.value();
        count++;
        holdIn( active, delay);
    }

    return *this;
        
        
}

/*** the internalFunction ***/
Model &PCS_tank_storage_18_model::internalFunction( const InternalMessage &msg )
{

           
    passivate();
    return *this;
        
        
}

/*** the outputFunction ***/
Model &PCS_tank_storage_18_model::outputFunction( const InternalMessage &msg )
{

        
    if(count == 2)
    {
        if(PCS_tank_18_stream_GB90_out_flowrate > (PCS_tank_18_stream_GB90_inv_val + PCS_tank_18_stream_GB90_1_flowrate))
            PCS_tank_18_stream_GB90_out_flowrate = PCS_tank_18_stream_GB90_inv_val + PCS_tank_18_stream_GB90_1_flowrate;
        PCS_tank_18_stream_GB90_2_flowrate = PCS_tank_18_stream_GB90_out_flowrate;
        PCS_tank_18_stream_GB90_inv_val = PCS_tank_18_stream_GB90_inv_val + PCS_tank_18_stream_GB90_1_flowrate - PCS_tank_18_stream_GB90_out_flowrate;
        sendOutput( msg.time(), PCS_tank_18_stream_GB90_2, PCS_tank_18_stream_GB90_2_flowrate);
        sendOutput( msg.time(), PCS_tank_18_stream_GB90_inv, PCS_tank_18_stream_GB90_inv_val);
        count = 0;
    }
    return *this;
        
        
}

